﻿using Android_Helper.entity;
using Android_Helper.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Android_Helper.antDegign
{
    class DescTableUtils
    {
        public static string AntDesignReactCRUD(string connectiongString, string dbName, string tableName)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(AntDesignService(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(AntDesignInterfaceTableStruct(connectiongString, dbName, tableName));
            stringBuilder.AppendLine(AntDesignProTable(connectiongString, dbName, tableName));
            //stringBuilder.AppendLine(GolangRowsToObject(connectiongString, dbName, tableName));
            //stringBuilder.AppendLine(GolangGetRow(connectiongString, dbName, tableName));
            //stringBuilder.AppendLine(GolangGetRows(connectiongString, dbName, tableName));
            //stringBuilder.AppendLine(GolangUpdateOrDelete(connectiongString, dbName, tableName));
            //stringBuilder.AppendLine(GolangInsert(connectiongString, dbName, tableName));
            //stringBuilder.AppendLine(GolangUpdateSQLAndParamsSegment(connectiongString, dbName, tableName));
            return stringBuilder.ToString();
        }

        private static string AntDesignService(string connectiongString, string dbName, string tableName)
        {
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("export async function find" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "ByStockCode(params?: any) {");
            stringBuilder.AppendLine("    return request('/api/BSStockStasticsMainAddStorage/findByStockCode', {");
            stringBuilder.AppendLine("            method: 'POST',");
            stringBuilder.AppendLine("            data:");
            stringBuilder.AppendLine("            {...params,");
            stringBuilder.AppendLine("             method: 'post',");
            stringBuilder.AppendLine("              },");
            stringBuilder.AppendLine("          });");
            stringBuilder.AppendLine("}");
            return stringBuilder.ToString();
        }

        private static string AntDesignProTable(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("interface " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "TableProps{");
            stringBuilder.AppendLine("  dataSource: any");
            stringBuilder.AppendLine("  visibleColumns: string;");
            stringBuilder.AppendLine("  onRowChanged: (row: any) => void;");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("const "+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "Table: React.FC<"+ NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "TableProps>=(props)=>{");
            stringBuilder.AppendLine("const [currentRow, setCurrentRow] = useState<" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "TableListItem>();");
            stringBuilder.AppendLine("const actionRef = useRef<ActionType>();");
            stringBuilder.AppendLine("const columns: ProColumns<" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "TableListItem>[] = [");
            for(int colPos = 0; colPos< list.Count; colPos++)
            {
                stringBuilder.AppendLine("{");
                stringBuilder.AppendLine("title: '" + list[colPos].ColumnComment + "',");
                stringBuilder.AppendLine("dataIndex: '" + NameUtils.LowercaseUnderlineToCammle(list[colPos].ColumnName) + "',");
                stringBuilder.AppendLine("hideInTable: props.visibleColumns&&!props.visibleColumns.includes('" + NameUtils.LowercaseUnderlineToCammle(list[colPos].ColumnName) + ",'),");
                if (AntDesignReactTypeUtils.DbTypeToES6React(list[colPos].ColumnType) == "number")
                {
                    stringBuilder.AppendLine("sorter: (a, b) => a." + NameUtils.LowercaseUnderlineToCammle(list[colPos].ColumnName) + "-b." + NameUtils.LowercaseUnderlineToCammle(list[colPos].ColumnName) + ",");
                }
                else {
                    stringBuilder.AppendLine("sorter: (a, b) => a." + NameUtils.LowercaseUnderlineToCammle(list[colPos].ColumnName) + ".localeCompare(b." + NameUtils.LowercaseUnderlineToCammle(list[colPos].ColumnName) + "),");
                }
                stringBuilder.AppendLine("},");
            }
            stringBuilder.AppendLine("]");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("return (");
            stringBuilder.AppendLine("<div>");
            stringBuilder.AppendLine("<ProTable<" + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "TableListItem>");
            stringBuilder.AppendLine("headerTitle='"+ tableEntity.ColumnComment + "'");
            stringBuilder.AppendLine("rowKey='id'");
            stringBuilder.AppendLine("actionRef={actionRef}");
            stringBuilder.AppendLine("pagination={{pageSize:5}}");
            stringBuilder.AppendLine("options={{ fullScreen:true }}");
            stringBuilder.AppendLine("toolBarRender={() => []}");
            stringBuilder.AppendLine("dataSource={props.dataSource}");
            stringBuilder.AppendLine("columns={columns}");
            stringBuilder.AppendLine("search={false}");
            stringBuilder.AppendLine("rowSelection={{");
            stringBuilder.AppendLine("type: 'radio',");
            stringBuilder.AppendLine("columnTitle: '选择',");
            stringBuilder.AppendLine("onSelect: (selectedRowKeys) => {");
            stringBuilder.AppendLine("if (props.onRowChanged){");
            stringBuilder.AppendLine("    props.onRowChanged(selectedRowKeys)");
            stringBuilder.AppendLine("};");
            stringBuilder.AppendLine("setCurrentRow(selectedRowKeys);");
            stringBuilder.AppendLine("},");
            stringBuilder.AppendLine("/>");
            stringBuilder.AppendLine("</div>");
            stringBuilder.AppendLine(")");
            stringBuilder.AppendLine("export default " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "Table;");
            return stringBuilder.ToString();
        }

        public static string AntDesignInterfaceTableStruct(string connectiongString, string dbName, string tableName)
        {
            List<TableColumnEntity> list = SchemaUtils.TableColumnToEntity(connectiongString, dbName, tableName);
            TableColumnEntity tableEntity = SchemaUtils.TableToEntity(connectiongString, dbName, tableName);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("// " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + " " + tableEntity.ColumnComment);
            stringBuilder.AppendLine("export interface " + NameUtils.LowercaseUnderlineToAllCapitalUpper(tableName) + "TableListItem {");
            foreach (TableColumnEntity tableColumn in list)
            {
                stringBuilder.AppendLine(NameUtils.LowercaseUnderlineToCammle(tableColumn.ColumnName) + ": " + AntDesignReactTypeUtils.DbTypeToES6React(tableColumn.ColumnType) + "  // " + tableColumn.ColumnComment);
            }
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine();
            return stringBuilder.ToString();
        }

    }
}
